package MyArrayLst;

import java.util.Arrays;

public class MyArrayList implements MyArrayListOperations{

    public int[] array;

    private int usedSize;//已经存放的数据个数

    private static final int DEFAULT_CAPACITY = 10;//顺序表容量

    public MyArrayList() {
        this.array = new int[DEFAULT_CAPACITY];
    }

    @Override
    public void add(int data) {
        if (this.isFull()) {
            this.growCapacity();
        }

        this.array[this.usedSize] = data;
        this.usedSize++;
    }

    @Override
    public boolean isFull() {
        return this.usedSize == MyArrayList.DEFAULT_CAPACITY;
    }

    @Override
    public void add(int pos, int data) {

        try {
            isPosLegitimate(pos);
            isEmptyArray();

            if (this.isFull()) {
                this.growCapacity();
            }

            for (int i = this.usedSize - 1; i >= pos; i--) {
                this.array[i + 1] = this.array[i];
            }

            this.array[pos] = data;

            this.usedSize++;

        } catch (IsPosLegitimate e) {
            e.printStackTrace();
        } catch (IsEmptyArray e) {
            e.printStackTrace();
        }

    }

    //判断顺序表是否为空
    public void isEmptyArray() {
        if (this.usedSize == 0) {
            throw new IsEmptyArray("顺序表为空");
        }
    }

    //给顺序表扩容
    private void growCapacity() {
        this.array = Arrays.copyOf(this.array, MyArrayList.DEFAULT_CAPACITY * 2);
    }

    //判断下标是否合法
    private void isPosLegitimate(int pos) {

        if (pos < 0 || pos > this.usedSize) {
            throw new IsPosLegitimate("下标不合法");
        }

    }

    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if (this.array[i] == toFind) {
                return true;
            }
        }

        return false;
    }

    @Override
    public int indexOf(int toFind) {
        try {
            isEmptyArray();

            for (int i = 0; i < this.usedSize; i++) {
                if (this.array[i] == toFind) {
                    return i;
                }
            }

        } catch (IsEmptyArray e) {
            e.printStackTrace();
        }

        return -1;
    }

    @Override
    public int get(int pos) {

        try {
            isPosLegitimate2(pos);
            isEmptyArray();

            return this.array[pos];
        } catch (IsPosLegitimate e) {
            e.printStackTrace();
        } catch (IsEmptyArray e) {
            e.printStackTrace();
        }

        return -1;//找不到就返回-1
    }

    public void isPosLegitimate2(int pos) {
        if (pos < 0 || pos >= this.usedSize) {
            throw new IsPosLegitimate("下标不合法");
        }
    }

    @Override
    public void set(int pos, int value) {

        try {
            isPosLegitimate2(pos);
            isEmptyArray();

            this.array[pos] = value;
        } catch (IsPosLegitimate e) {
            e.printStackTrace();
        } catch (IsEmptyArray e) {
            e.printStackTrace();
        }
    }

    @Override
    public void remove(int toRemove) {

        try {
            isEmptyArray();

            int pos = 0;

            for (int i = 0; i < this.usedSize; i++) {
                if (this.array[i] == toRemove) {
                    pos = i;
                }
            }

            for (int i = pos; i <= this.usedSize - 1; i++) {
                this.array[i] = this.array[i + 1];
            }

            this.usedSize--;

        } catch (IsEmptyArray e) {
            e.printStackTrace();
        }

    }

    @Override
    public int size() {
        return this.usedSize;
    }

    @Override
    public void clear() {

        for (int i = 0; i < this.usedSize; i++) {
            this.array[i] = 0;
        }

        this.usedSize = 0;

    }

    @Override
    public void display() {

        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(array[i] + " ");
        }

        System.out.println();

    }
}
